package com.example.controller.adm;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import com.example.service.IVehicleService;
import com.example.util.GetResourcesFile;
import com.example.util.ProKeyGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import com.example.pojo.Vehicle;

import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 * 车辆信息表 前端控制器
 * </p>
 *
 * @author wsl
 * @since 2022-05-06
 */
@RestController
@RequestMapping("/vehicle")
@Api(tags = "车辆信息表",value = "")
@CrossOrigin(allowCredentials = "true")
public class VehicleController {

    @Autowired
    private IVehicleService vehicleService;

    @Resource
    private GetResourcesFile getResourcesFile;

    @ApiOperation(value = "查询车辆信息表")
    @GetMapping("/query")
    public PageInfo<Vehicle> getVehiclePageInfo(String brand,
                                                Integer type,
                                                @RequestParam(defaultValue = "1") Integer pageNum,
                                                @RequestParam(defaultValue = "5") Integer pageSize,
                                                Integer deleted) {
        return vehicleService.getVehiclePageInfo(brand,type,pageNum,pageSize,0);
    }

    @ApiOperation(value = "新增车辆信息表")
    @PostMapping("/add")
    public ResultVo addVehicle(@RequestBody Vehicle vehicle) {
        if (vehicleService.save(vehicle)) {
            return ResultVo.success("新增车辆成功");
        }
        return ResultVo.error("新增车辆失败");
    }
    @ApiOperation(value = "修改车辆信息表")
    @PutMapping("/update")
    public ResultVo updateVehicle(@RequestBody Vehicle vehicle) {
        if (vehicleService.updateById(vehicle)) {
            return ResultVo.success("修改成功。");
        }
        return ResultVo.error("修改失败!");
    }

    @ApiOperation(value = "删除车辆信息表")
    @DeleteMapping("/delete")
    public ResultVo deleteVehicleByid(Integer[] ids){
        if(vehicleService.removeByIds(Arrays.asList(ids))){
            return ResultVo.success("删除成功");
        }
        return ResultVo.error("删除失败");
    }
    @ApiOperation(value = "可使用的车辆信息")
    @GetMapping("/enabledquery")
    public ResultVo selectCarNmae(){
        return vehicleService.selectCarNmae() ;
    }

    @PostMapping("insert_img")
    public Boolean insertImg(MultipartFile multipartFile){
        String resourcesFile = getResourcesFile.getResourcesFile();
        upload(multipartFile,resourcesFile);

        return true;
    }

    public String upload(MultipartFile file,String path) {
        String s = ProKeyGenerator.generate("img");

        String filename = null;
        if(!file.isEmpty()) {
            //上传文件名
            filename = s + file.getOriginalFilename();

            File filepath = new File(path,filename);
            //判断路径是否存在，如果不存在就创建一个
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }
            //将上传文件保存到一个目标文件当中
            try {
                file.transferTo(new File(path + File.separator + filename));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return filename;
    }
}
